﻿using System;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Validation.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;

namespace AsNum.Common.EntLib.Validation.Adapters {
    public class RelativeDateTimeClientAdapter : BaseClientAdapter<RelativeDateTimeValidatorData> {

        public override RuleTypes RuleType {
            get {
                return RuleTypes.RelativeDateTime;
            }
        }

        public RelativeDateTimeClientAdapter(RelativeDateTimeValidatorData data , Dictionary<string , Tuple<string , string>> map , bool isSub = false)
            : base ( data , map , isSub ) {

            this.AddRule ( RuleKeys.ServerTime , string.Format ( "'{0}'" , DateTime.Now.ToString ( "yyyy-MM-dd HH:mm:ss" ) ) );

            if ( this.ValidatorData.LowerBoundType != RangeBoundaryType.Ignore ) {
                this.AddRule ( RuleKeys.Min , this.ValidatorData.LowerBound.ToString ( ) );
                this.AddRule ( RuleKeys.IncludeMin , ( this.ValidatorData.LowerBoundType == RangeBoundaryType.Inclusive ).JsBool ( ) );
                this.AddRule ( RuleKeys.MinUnit , string.Format ( "'{0}'" , GetUnit ( this.ValidatorData.LowerUnit ) ) );
            }

            if ( this.ValidatorData.UpperBoundType != RangeBoundaryType.Ignore ) {
                this.AddRule ( RuleKeys.Max , this.ValidatorData.UpperBound.ToString ( ) );
                this.AddRule ( RuleKeys.IncludeMax , ( this.ValidatorData.LowerBoundType == RangeBoundaryType.Inclusive ).JsBool ( ) );
                this.AddRule ( RuleKeys.MaxUnit , string.Format ( "'{0}'" , GetUnit ( this.ValidatorData.UpperUnit ) ) );
            }
        }

        //yy:year, m:month, d: date,h:hour, mi:minute, s:second, ms:毫秒
        //TODO None 是用来干什么的？
        private string GetUnit(DateTimeUnit unit) {
            switch ( unit ) {
                case DateTimeUnit.Day:
                    return "d";
                case DateTimeUnit.Hour:
                    return "h";
                case DateTimeUnit.Minute:
                    return "mi";
                case DateTimeUnit.Month:
                    return "m";
                case DateTimeUnit.Second:
                    return "s";
                case DateTimeUnit.Year:
                    return "yy";
                default:
                    return "";
            }
        }
    }
}
